package GUI;

import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.TextArea;

import javax.swing.JTextField;

import Client_logic.Template;
import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Product;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

/**
 * This class use for show and update Template.
 * @author sharon
 * @author michael
 * @author matan
 * @author ido
 *
 */
public class TemplateForm extends JFrame
{
	private JLabel pIDjLabel = null;
	private JLabel pnamejLabel = null;
	private JLabel pPricejLabel = null;
	private JLabel pdescriptionjLabel = null;
	private JTextField pIDjTextField = null;
	private JTextField pnamejTextField = null;
	private JTextField pPricejTextField = null;
	private TextArea pdescriptionjTextArea = null;
	private JButton createjButton = null;
	private JButton BackjButton = null;
	private JButton updateButton = null;
	private JLabel templateIDjLabel = null;
	private JLabel templatenamejLabel = null;
	private JLabel clientmarketjLabel = null;
	private JComboBox clientmarkeJComboBox = null;
	private JLabel messagetoclientjLabel = null;
	private TextArea messagetoclientTextArea = null;
	private JTextField templateIDjTextField = null;
	private JTextField templatenamejTextField = null;
	private JComboBox pIDComboBox = null;
	private Template template = null;
	private JLabel pCNumlLabel = null;
	
	/**
	 * This is default constructor
	 */
	public TemplateForm()
	{
		super();
		setIconImage(Toolkit.getDefaultToolkit().getImage(TemplateForm.class.getResource("/javax/swing/plaf/metal/icons/ocean/hardDrive.gif")));
		getContentPane().setBackground(SystemColor.activeCaption);
		setBackground(SystemColor.activeCaption);
		initialize();
	}
	
	/**
	 * This method initializes TemplateForm
	 */
	private void initialize() 
	{
		templateIDjLabel = new JLabel();
		templateIDjLabel.setBounds(new Rectangle(17, 20, 107, 23));
		templateIDjLabel.setText("Template number");
		
		templatenamejLabel = new JLabel();
		templatenamejLabel.setBounds(new Rectangle(17, 50, 107, 23));
		templatenamejLabel.setText("Template name");
		
		clientmarketjLabel = new JLabel();
		clientmarketjLabel.setBounds(new Rectangle(17, 80, 107, 23));
		clientmarketjLabel.setText("Client market");
				
		messagetoclientjLabel = new JLabel();
		messagetoclientjLabel.setBounds(new Rectangle(17, 110, 107, 23));
		messagetoclientjLabel.setText("Message to client");
		
		pdescriptionjLabel = new JLabel();
		pdescriptionjLabel.setBounds(new Rectangle(270, 110, 107, 23));
		pdescriptionjLabel.setText("Description");
		
		pPricejLabel = new JLabel();
		pPricejLabel.setBounds(new Rectangle(270, 80, 107, 23));
		pPricejLabel.setText("Price");
		
		pnamejLabel = new JLabel();
		pnamejLabel.setBounds(new Rectangle(270, 50, 107, 23));
		pnamejLabel.setText("Name");
		
		pIDjLabel = new JLabel();
		pIDjLabel.setBounds(new Rectangle(270, 20, 107, 23));
		pIDjLabel.setText("Category number");
		
		template = new Template(0,"",null,"","" );
		
		getContentPane().setLayout(null);
		this.setSize(524, 277);
		getContentPane().add(pIDjLabel, null);
		getContentPane().add(pnamejLabel, null);
		getContentPane().add(pPricejLabel, null);
		getContentPane().add(pdescriptionjLabel, null);
		getContentPane().add(getPIDjTextField(), null);
		getContentPane().add(getPnamejTextField(), null);
		getContentPane().add(getPPricejTextField(), null);
		getContentPane().add(getPdescriptionjTextArea(), null);
		getContentPane().add(getBackjButton(), null);
		getContentPane().add(templateIDjLabel, null);
		getContentPane().add(templatenamejLabel, null);
		getContentPane().add(clientmarketjLabel, null);
		getContentPane().add(messagetoclientjLabel, null);
		getContentPane().add( getMessageToClientTextArea(), null);
		getContentPane().add(getCampainIDjTextField(), null);
		getContentPane().add(getTemplatenamejTextField(), null);	
		getContentPane().add(getClientMarkeJComboBox(), null);
		this.setTitle("Set Template");
	}	
	
	/**
	 * This method initializes pIDjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPIDjTextField() {
		if (pIDjTextField == null) {
			pIDjTextField = new JTextField();
			pIDjTextField.setBounds(new Rectangle(380, 20, 120, 23));
			pIDjTextField.setEditable(false);
		}
		return pIDjTextField;
	}
	/**
	 * This method initializes pnamejTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPnamejTextField() {
		if (pnamejTextField == null) {
			pnamejTextField = new JTextField();
			pnamejTextField.setBounds(new Rectangle(380, 50, 120, 23));
			pnamejTextField.setEditable(false);
		}
		return pnamejTextField;
	}
	/**
	 * This method initializes pPricejTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPPricejTextField() {
		if (pPricejTextField == null) {
			pPricejTextField = new JTextField();
			pPricejTextField.setBounds(new Rectangle(380, 80, 120, 23));
			pPricejTextField.setEditable(false);
		}
		return pPricejTextField;
	}
	/**
	 * This method initializes pdescriptionjTextArea	
	 * 	
	 * @return java.awt.TextArea	
	 */
	private TextArea getPdescriptionjTextArea() {
		if (pdescriptionjTextArea == null) {
			pdescriptionjTextArea = new TextArea();
			pdescriptionjTextArea.setBounds(new Rectangle(380, 110, 120, 70));
			pdescriptionjTextArea.setEditable(false);
		}
		return pdescriptionjTextArea;
	}
	/**
	 * This method initializes nextjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCreatejButton() {
		if (createjButton == null)
		{
			createjButton = new JButton();
			createjButton.setBounds(new Rectangle(260, 220, 100, 25));
			createjButton.setText("Create");
			createjButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String stready = updateTemplate();
					if (stready.equals("Ready"))
					{
						try
						{	
							String message = template.saveTemplate();
							if (message == null)
								JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);	
							else
							{
								if (message.equals("OK"))
									JOptionPane.showMessageDialog(null, "Template succsesfully creaded");
								else
									JOptionPane.showMessageDialog(null, "Ilegal template number");
							}
						}catch(ClientNotInitializesException cNIexception)
						{
							JOptionPane.showMessageDialog(null, "ERROR","CCRM ERROR",JOptionPane.ERROR_MESSAGE);	
						}
						catch(Exception exception)
						{
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);	
						}	
					}								
				}
			});		
		}
		return createjButton;
	}
	/**
	 * This method initializes BackjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getBackjButton() 
	{
		if (BackjButton == null) 
		{
			BackjButton = new JButton();
			BackjButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			BackjButton.setBounds(new Rectangle(10, 203, 76, 25));
			BackjButton.setText("Back");
		}
		return BackjButton;
	}
		
	/**
	 * This method initializes templateIDjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCampainIDjTextField() {
		if (templateIDjTextField == null) {
			templateIDjTextField = new JTextField();
			templateIDjTextField.setBounds(new Rectangle(130, 20, 120, 23));
		}
		return templateIDjTextField;
	}
	
	/**
	 * This method initializes templatenamejTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTemplatenamejTextField() {
		if (templatenamejTextField == null) {
			templatenamejTextField = new JTextField();
			templatenamejTextField.setBounds(new Rectangle(130, 50, 120, 23));
		}
		return templatenamejTextField;
	}
	
	/**
	 * This method initializes clientmarkeJComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getClientMarkeJComboBox() {
		if (clientmarkeJComboBox == null) {
			clientmarkeJComboBox = new JComboBox();
			clientmarkeJComboBox.setBounds(new Rectangle(130, 80, 120, 23));
			String[] clientmarkek = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				clientmarkek = (String[]) connectiontoserver.loadAllClientsMarket();
				if (clientmarkek == null)
				{
					JOptionPane.showMessageDialog(null, "ERROR!!!");
					clientmarkeJComboBox.addItem("");
				}	
				else
				{
					int len = clientmarkek.length;
					for(int i=0; i < len; i++)
					{
						clientmarkeJComboBox.addItem(clientmarkek[i]);
					}	
				}
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				clientmarkeJComboBox.addItem("");
			}
			clientmarkeJComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					
				}	
			});	
		}
		return clientmarkeJComboBox;
	}
	
	/**
	 * This method initializes messagetoclientTextArea	
	 * 	
	 * @return TextArea
	 */
	private TextArea getMessageToClientTextArea() {
		if (messagetoclientTextArea == null) {
			messagetoclientTextArea = new TextArea();
			messagetoclientTextArea.setBounds(new Rectangle(130, 110, 120, 70));
			
		}
		return messagetoclientTextArea;
	}
	
	
	/**
	 * This method load template details to the FORM 
	 * @param template
	 */
	public void loadTemplate(Template template)
	{
		long cID = template.getTemplateID();
		if (cID == 0)
			templateIDjTextField.setText("");
		else
			templateIDjTextField.setText(String.valueOf(cID));
		templatenamejTextField.setText(template.getTemplateName());
		clientmarkeJComboBox.setSelectedItem(template.getClientmarket());
		messagetoclientTextArea.setText(template.getMsg());
		pIDjTextField.setText(String.valueOf(template.getProduct().getCategoryID()));
		pnamejTextField.setText(template.getProduct().getName());
		pPricejTextField.setText(String.valueOf(template.getProduct().getPrice()));
		pdescriptionjTextArea.setText(template.getProduct().getDescription());		
	}
	
	/**
	 * This method update customer to data from the form
	 */
	private String updateTemplate()
	{
		try 
		{
			long cid = Long.parseLong(templateIDjTextField.getText());
			String cname = templatenamejTextField.getText();
			String msg =  messagetoclientTextArea.getText();;
			String  sector = (String)clientmarkeJComboBox.getSelectedItem();
			
			if (cid > 0 && !cname.equals("") && !sector.equals("") && !msg.equals(""))
			{
				double price = Double.parseDouble(pPricejTextField.getText());
				String pname = pnamejTextField.getText();
				String description = pdescriptionjTextArea.getText();
				long pID = Long.parseLong(pIDjTextField.getText());
				Product product = new Product((int)pID, price, pname, description, pID);
				template.setTemplateID(cid);
				template.setTemplateName(cname);
				template.setMsg(msg);
				template.setClientmarket(sector);
				template.setProduct(product);
				return "Ready";
			}							
			else
			{
				JOptionPane.showMessageDialog(null, "Requested fields not filled");
				return "ERROR";
			}			
		}
		catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT","",JOptionPane.WARNING_MESSAGE);
			return "ERROR";
		}	
	}
	
	/**
	 * This method initializes updateButton	
	 */
	private JButton getUpdateButton() 
	{
		if (updateButton == null) 
		{
			updateButton = new JButton();
			updateButton.setBounds(new Rectangle(260, 220, 100, 25));
			updateButton.setText("Update");
			updateButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String stready = updateTemplate();
					if (stready.equals("Ready"))
					{
						try
						{
							String msgfromserver = template.updateTemplate();
							if (msgfromserver != null)
							{
								if (msgfromserver.equals("OK"))
									JOptionPane.showMessageDialog(null, "The template successfully update");
								else
									JOptionPane.showMessageDialog(null, "The template not update!!!","WARNING", JOptionPane.WARNING_MESSAGE);
							}
							else
								JOptionPane.showMessageDialog(null, "ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
						}catch (ClientNotInitializesException excp)
						{
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
						}	
					}		
				}					
			});						
		}
		return updateButton;
	}
	
	/**
	 * This method initializes PIDComboBox	
	 */
	private JComboBox getpIDComboBox()
	{
		if(pIDComboBox == null)
		{	
			pIDComboBox = new JComboBox();
			pIDComboBox.setBounds(new Rectangle(380, 170, 107, 25));
			long[] productid = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				productid = (long[]) connectiontoserver.loadAllProductsID();
				if (productid == null)
				{
					JOptionPane.showMessageDialog(null, "Catalogue is EMPTY!!!");
					pIDComboBox.addItem(0);
				}	
				else
				{
					int len = productid.length;
					for(int i=0; i < len; i++)
					{
						pIDComboBox.addItem(productid[i]);
					}
					pIDComboBox.setSelectedItem(pIDjTextField.getText());
				}
			}catch(Exception e)
			{
				pIDComboBox.addItem(0);
			}
					
			pIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					Product product = new Product(0, 0,"","", 0);
					long pID = (Long)pIDComboBox.getSelectedItem();	
					try 
					{
						if (product.loadProduct(pID))
						{
							pIDjTextField.setText(String.valueOf(product.getCategoryID()));
							pnamejTextField.setText(product.getName());
							pPricejTextField.setText(String.valueOf(product.getPrice()));
							pdescriptionjTextArea.setText(product.getDescription());
						}
						else
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);						
					} catch (ClientNotInitializesException e1) 
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
					}
				}	
			});	
		}	
		return pIDComboBox;
	}
	
	/**
	 * This method added "Create Button" to the FORM
	 */
	public void addCreateButton()
	{
		getContentPane().add(getCreatejButton(), null);
	}	
	
	/**
	 * This method added "Back Button" to the FORM
	 */
	public void addBackButton()
	{
		getContentPane().add(getBackjButton(), null);
	}
	
	/**
	 * This method added "Update Button" to the FORM
	 */
	public void addUpdateButton()
	{
		getContentPane().add(getUpdateButton(), null);
		getContentPane().add(getpIDComboBox(), null);
		pCNumlLabel = new JLabel();
		pCNumlLabel.setBounds(new Rectangle(270, 170, 107, 23));
		pCNumlLabel.setText("Catalogue number");
		Color color = new Color(1000);
		pCNumlLabel.setForeground(color);
		getContentPane().add(pCNumlLabel, null);
		templateIDjTextField.setEditable(false);
	}

	/**
	 * @return the template
	 */
	public Template getTemplate() 
	{
		return template;
	}
	
	/**
	 * This method close all TextField to Editables.
	 */
	public void closeall()
	{
		templateIDjTextField.setEditable(false);
		templatenamejTextField.setEditable(false);
		clientmarkeJComboBox.setEditable(false);
		messagetoclientTextArea.setEditable(false);		
		clientmarkeJComboBox.setEnabled(false);
		
	}
	
	

	
	
}
